package com.cj.mobile.iabp.app.dialog;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cj.mobile.iabp.R;
import com.cj.mobile.iabp.app.utils.RecycleViewDivider;
import com.cj.mobile.iabp.mvp.ui.adapter.SelectTitleCommonAdapter;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.utils.ArmsUtils;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * 包名： com.zqw.mobile.aptitude.app.dialog
 * 对象名： PopupTitleCommonList
 * 描述：通用的选择列表，带title
 * 作者： wly
 * 邮箱：wangliyang206@163.com
 * 创建日期： 2020/9/7 14:48
 */

public class PopupTitleCommonList extends PopupWindow implements DefaultAdapter.OnRecyclerViewItemClickListener {

    @Override
    public void onItemClick(@NotNull View view, int viewType, @NotNull Object data, int position) {
        String info = (String) data;
        if (itemClick != null) {
            itemClick.itemClick(info);
        }
        this.dismiss();
    }

    public interface ItemClick {
        void itemClick(String info);
    }

    private ItemClick itemClick;

    public PopupTitleCommonList(Context context, String title, List<String> mList, ItemClick itemClick) {
        super(context);
        this.itemClick = itemClick;

        //初始化下拉列表
        View view = LayoutInflater.from(context).inflate(R.layout.pop_select_commlist_layout, null);
        setContentView(view);
        setWidth(LinearLayout.LayoutParams.MATCH_PARENT);//LinearLayout.LayoutParams.MATCH_PARENT
        setHeight(LinearLayout.LayoutParams.MATCH_PARENT);

        // 设置背景色
        setBackgroundDrawable(new ColorDrawable());

        // 让popwin获取焦点
        setFocusable(true);
        // 点击弹出窗口区域之外的任意区域，则该窗口关闭
        setOutsideTouchable(true);

        TextView tips = view.findViewById(R.id.txvi_popview_tips);
        tips.setText("请选择" + title);

        RecyclerView recyclerView = view.findViewById(R.id.list_popview_content);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context);
        ArmsUtils.configRecyclerView(recyclerView, mLayoutManager);
        //设置分隔线
        recyclerView.addItemDecoration(new RecycleViewDivider(context, LinearLayoutManager.VERTICAL, 1, ContextCompat.getColor(context, R.color.line_frame_color)));
        //设置Adapter
        SelectTitleCommonAdapter recycleAdapter = new SelectTitleCommonAdapter(mList);
        recyclerView.setAdapter(recycleAdapter);
        recycleAdapter.setOnItemClickListener(this);
    }
}
